﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DP.Web.UI;
using CRM.Entity;
using CRM.BLL;
using DP.Common;

namespace CRM.Web.Pop
{
    public partial class CallInPopUpPage : Common.PageBase
    {
        private const string frameWorkOrderInfo = "WorkOrder.aspx?PHONE={0}&CALLID={1}&CALLED={2}&CALLTYPE={3}&CUSTOMERID={4}&CUSTOMERCODE={5}&RECORDCODE={6}&VINCODE={7}";
        private const string frameVehicleInfo = "VehicleInfo.aspx?PHONE={0}&CALLID={1}&CALLED={2}&CALLTYPE={3}&CUSTOMERID={4}&CUSTOMERCODE={5}&RECORDCODE={6}&VINCODE={7}";
        private const string frameCustomerInfo = "CustomerInfo.aspx?MOBILEPHONE={0}&VINCODE={1}";
        private const string frameHistoryWork = "HistoryWork.aspx?CALLER={0}&CALLID={1}&CALLED={2}&CALLTYPE={3}&CUSTOMERID={4}&CUSTOMERCODE={5}&RECORDCODE={6}&VINCODE={7}";
        private string frameFAQInfo =  "../FaqManage/FaqRightListPop.aspx?CATID=-9&CATNAME=全文检索&SRCKEY=";

        private T_CUSTOMERTELINDEX_BLL customerTelIndexBll = new T_CUSTOMERTELINDEX_BLL();
        private T_PHONEAREA_BLL phoneAreaBll = new T_PHONEAREA_BLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            frameFAQInfo = "../FaqManage/FaqRightListPop.aspx?CATID=-9&CATNAME=" + Server.UrlEncode("全文检索") + "&SRCKEY=";

            //LogHelper.WriteLog("Log_CallBackEvent", String.Format("DateTime:{1},Page:{0},Method:{2}",DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), "CallInPopUpPage", "PageLoad" ));
            if(!IsPostBack)
            {
                pageInit();
            }
        }

      

        /// <summary>
        /// Pages the init.
        /// </summary>
        private void pageInit()
        {
            string _phone = PageHelper.GetQueryString(this.Page, "PHONE");
            string _callId = PageHelper.GetQueryString(this.Page, "CALLID");
            string _called = PageHelper.GetQueryString(this.Page, "CALLED");
            string _callType = PageHelper.GetQueryString(this.Page, "CALLTYPE");
            string _recordCode = PageHelper.GetQueryString(this.Page, "RECORDCODE");
            string _vinCode = PageHelper.GetQueryString(this.Page, "VINCODE");

            lblTitle.Text = String.Format("客户来电弹屏");            
            _phone = UserLoginInfo.FilterTelPhone(_phone);
            lblTel.Text = _phone;

            T_PHONEAREA_Entity phoneareaEntity;
            string prePhone;
            int phoneLenght;
            if (phoneAreaBll.CheckPhoneType(_userLoginInfo))
            {
                phoneLenght = 4;
                prePhone = (_phone.Length > 4) ? _phone.Substring(0, phoneLenght) : _phone;
                phoneareaEntity = phoneAreaBll.GetEntity("AREANO", prePhone);
                if (phoneareaEntity != null)
                {
                    lblPhoneArea.Text = phoneareaEntity.COUNTRY;
                }
            }
            else
            {
                phoneLenght = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PhoneAreaLenght"), 7);
                if (_phone.IndexOf("13") == 0 || _phone.IndexOf("15") == 0 || _phone.IndexOf("14") == 0 || _phone.IndexOf("18") == 0)
                {
                    prePhone = (_phone.Length > phoneLenght) ? _phone.Substring(0, phoneLenght) : _phone;
                    phoneareaEntity = phoneAreaBll.GetEntity("sectionno", prePhone);
                    if (phoneareaEntity != null)
                    {
                        lblPhoneArea.Text = phoneareaEntity.PROVINCE + "-" + phoneareaEntity.CITY;
                    }
                }
                else if (_phone.IndexOf("01") == 0 || _phone.IndexOf("02") == 0)
                {
                    phoneLenght = 3;
                    prePhone = (_phone.Length > 3) ? _phone.Substring(0, phoneLenght) : _phone;
                    phoneareaEntity = phoneAreaBll.GetEntity("AREANO", prePhone);
                    if (phoneareaEntity != null)
                    {
                        lblPhoneArea.Text = phoneareaEntity.PROVINCE + "-" + phoneareaEntity.CITY;
                    }
                }
                else
                {
                    phoneLenght = 4;
                    prePhone = (_phone.Length > 4) ? _phone.Substring(0, phoneLenght) : _phone;
                    phoneareaEntity = phoneAreaBll.GetEntity("AREANO", prePhone);
                    if (phoneareaEntity != null)
                    {
                        lblPhoneArea.Text = phoneareaEntity.PROVINCE + "-" + phoneareaEntity.CITY;
                    }
                }
            }

            List<T_CUSTOMERTELINDEX_Entity> customerTelIndexList = new List<T_CUSTOMERTELINDEX_Entity>();
            if (!UserLoginInfo.IsAdministrator())
            {
                UserType userType = UserLoginInfo.GetCurrentUserType();
                switch (userType)
                {
                    case UserType.中心部门:
                    case UserType.供应商:
                    case UserType.市场部:
                        {
                            customerTelIndexList = customerTelIndexBll.GetList(String.Format(" and PHONE = '{0}' and  BUSINESSDIVISIONCODE = '{1}' ", _phone, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE), new StringBuilder());
                        }
                        break;
                    case UserType.服务商:
                        {
                            customerTelIndexList = customerTelIndexBll.GetList(String.Format(" and PHONE = '{0}' and  BUSINESSDIVISIONCODE = '{1}' and ( not exists (select 1 from  t_salevehicle sv  where sv.customercode = t_customertelindex.customercode) or customercode in (select customercode from t_salevehicle sv where sv.stationcode = '{2}'))   ", _phone, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE, UserLoginInfo.GetDepartmentCode(_userLoginInfo)), new StringBuilder());
                        }
                        break;
                    case UserType.经销商:
                        {
                            customerTelIndexList = customerTelIndexBll.GetList(String.Format(" and PHONE = '{0}' and  BUSINESSDIVISIONCODE = '{1}' and ( not exists (select 1 from  t_salevehicle sv  where sv.customercode = t_customertelindex.customercode) or customercode in (select customercode from t_salevehicle sv where sv.dealercode = '{2}'))   ", _phone, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE, UserLoginInfo.GetDepartmentCode(_userLoginInfo)), new StringBuilder());
                        }
                        break;
                }
            }
            else
            {
                customerTelIndexList = customerTelIndexBll.GetList(String.Format(" and PHONE = '{0}' and  BUSINESSDIVISIONCODE = '{1}' ", _phone, _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE), new StringBuilder());
            }

            string _customerId = (customerTelIndexList.Count ==0) ? "" : customerTelIndexList[0].CUSTOMERID;
            string _customerCode = (customerTelIndexList.Count == 0) ? "" : customerTelIndexList[0].CUSTOMERCODE;

            frameWorkOrder.Attributes["src"] = String.Format(frameWorkOrderInfo, _phone, _callId, _called, _callType, _customerId, _customerCode, _recordCode, _vinCode);
            frameCustomer.Attributes["src"] = String.Format(frameCustomerInfo, _phone, _vinCode);
            frameHistory.Attributes["src"] = String.Format(frameHistoryWork, _phone, _callId, _called, _callType, _customerId, _customerCode, _recordCode, _vinCode);
            frameVehicle.Attributes["src"] = String.Format(frameVehicleInfo, "", _callId, _called, _callType, _customerId, _customerCode, _recordCode, _vinCode);
            frameFAQ.Attributes["src"] = String.Format(frameFAQInfo);
        }
    }
}
